package com.ruoyi.hospital.controller.storeInfo;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.domain.storeInfo.vo.PharmacyDrugNonVo;
import com.ruoyi.hospital.domain.storeInfo.vo.PharmacyDrugVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hospital.domain.storeInfo.PharmacyDruginfo;
import com.ruoyi.hospital.service.storeInfo.IPharmacyDruginfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品公共信息Controller------>药品信息的主表
 * 
 * @author wendao
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/hospital/druginfo")
public class PharmacyDruginfoController extends BaseController
{
    @Autowired
    private IPharmacyDruginfoService pharmacyDruginfoService;

    /**
     * 查询药品公共信息列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(PharmacyDruginfo pharmacyDruginfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            pharmacyDruginfo.setStoreId(getStoreId());
        }
        startPage();
        List<PharmacyDruginfo> list = pharmacyDruginfoService.selectPharmacyDruginfoList(pharmacyDruginfo);
        return getDataTable(list);
    }

    /**
     * 导出药品公共信息列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:export')")
    @Log(title = "药品公共信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PharmacyDruginfo pharmacyDruginfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            pharmacyDruginfo.setStoreId(getStoreId());
        }
        List<PharmacyDruginfo> list = pharmacyDruginfoService.selectPharmacyDruginfoList(pharmacyDruginfo);
        ExcelUtil<PharmacyDruginfo> util = new ExcelUtil<PharmacyDruginfo>(PharmacyDruginfo.class);
        util.exportExcel(response, list, "药品公共信息数据");
    }

    /**
     * 获取药品公共信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(pharmacyDruginfoService.selectPharmacyDruginfoById(id));
    }

    /**
     * 新增药品公共信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:add')")
    @Log(title = "药品公共信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PharmacyDruginfo pharmacyDruginfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            pharmacyDruginfo.setStoreId(getStoreId());
        }
        return toAjax(pharmacyDruginfoService.insertPharmacyDruginfo(pharmacyDruginfo));
    }

    /**
     * 修改药品公共信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:edit')")
    @Log(title = "药品公共信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PharmacyDruginfo pharmacyDruginfo)
    {
        return toAjax(pharmacyDruginfoService.updatePharmacyDruginfo(pharmacyDruginfo));
    }

    /**
     * 删除药品公共信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:remove')")
    @Log(title = "药品公共信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(pharmacyDruginfoService.deletePharmacyDruginfoByIds(ids));
    }



    //******************************************************************************************

    /*
    *
    * 获得药品主表+附表的所有信息
    * */
    @PreAuthorize("@ss.hasPermi('hospital:druginfo:query')")
    @GetMapping(value = "/getDrug/{id}")
    public AjaxResult getDrugInfo(@PathVariable("id") Integer id)
    {
        return success(pharmacyDruginfoService.getDrugInfo(id));
    }

    /*
     *
     * 获得非药品主表+附表的所有信息
     * */
    @GetMapping(value = "/getDrugNon/{id}")
    public AjaxResult getDrugInfoNon(@PathVariable("id") Integer id)
    {
        return success(pharmacyDruginfoService.getDrugInfoNon(id));
    }


    /*
     *
     * 获得非药品主表+附表的所有信息
     * */
    @GetMapping(value = "/getDrugHerb/{id}")
    public AjaxResult getDrugInfoHerb(@PathVariable("id") Integer id)
    {
        return success(pharmacyDruginfoService.getDrugInfoHerb(id));
    }

    /******************************************药品的发布********************************************************/
    /*
    * 1 发布药品信息
    * */
    @PostMapping("/releasedDrug")
    public AjaxResult releasedDrug(@RequestBody PharmacyDrugVo pharmacyDruginfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            pharmacyDruginfo.setStoreId(getStoreId());
        }
        pharmacyDruginfo.setDrugType(0);
        return toAjax(pharmacyDruginfoService.releasedPharmacyDrug(pharmacyDruginfo));
    }

    /*
     * 2 发布非药品信息
     * */
        @PostMapping("/releasedDrugNon")
    public AjaxResult releasedDrugNon(@RequestBody PharmacyDrugNonVo pharmacyDruginfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            pharmacyDruginfo.setStoreId(getStoreId());
        }
        pharmacyDruginfo.setDrugType(1);
        return toAjax(pharmacyDruginfoService.releasedPharmacyNonDrug(pharmacyDruginfo));
    }


    /*
     * 2 发布非药品信息
     * */
    @PostMapping("/releasedDrugHerb")
    public AjaxResult releasedDrugHerb(@RequestBody PharmacyDrugNonVo pharmacyDruginfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            pharmacyDruginfo.setStoreId(getStoreId());
        }
        pharmacyDruginfo.setDrugType(2);
        return toAjax(pharmacyDruginfoService.releasedPharmacyNonDrug(pharmacyDruginfo));
    }

}
